<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RoleModel
 *
 * @author wangnaixin
 */
namespace Admin\Model;
use Think\Model;
class RoleModel extends Model {
    //put your code here
    protected $tableName = 'role'; 
    
    public function getList($where=array()){
        return $this->where($where)->select();
    }
    
    /**
     * @todo  删除 用户组
     * @param int $roleId
     * @return boolean
     */
    public function delRole($roleId){
       
        return $this->where("id = ".$roleId)->delete();
    }
    
    public function getRoleInfoById($roleId){
        return $this->where("id = ".$roleId)->find();
    }
    
    /**
     * @todo 获取管理员用户组Ids
     */
    
    public function getAdminRoleIds(){
        $list = $this->where(array('status'=>1,'back_login'=>1))->select();
        $ids = [];
        foreach ($list as $v){
            $ids[] = $v['id'];
        }
        return $ids;
    }
    
    public function getRoleList(){
        return $this->where("status = 1")->select();
    }
    
    public function getRoleForRegister(){
    	return $this->where(array('status'=>1,'back_login'=>0))->find();
    }

    /**
     * 检查角色名称是否有重复
     * @param $name
     * @param $id
     * @return bool
     */
    public function checkExists($name,$id=0) {
        if (!$name) {
            return false;
        }
        $fields = array(
            'name' => $name
        );
        $pk = $id ? array('id' => $id) : array();
        return $this->isExists($this->tableName,$fields,$pk);
    }

}
